use crate::tool::Tool;
use tool_macro::tool;
use serde_json::{json, Value};
use anyhow::{Context, Result};
use async_trait::async_trait;

#[tool(
    struct_name = FileCreator,
    name = "create_file",
    description = "Create a new file",
    args = [
        "filepath" : {
            "type": "string",
            "description": "The file path",
        }
    ]
)]
pub fn create_file(filepath: &str) -> Result<()> {
    match std::fs::File::create(filepath) {
        Ok(_) => Ok(()),
        Err(err) => Err(err.into()),
    }
}

#[tool(
    struct_name = FileWritor,
    name = "write_file",
    description = 
r#"Write content to file.
If the file already exists, the original content will be overwritten directly."#,
    args = [
        "filepath" : {
            "type": "string",
            "description": "The file path to write",
        },
        "content" : {
            "type": "string",
            "description": "The content to write",
        },
    ]
)]
pub fn write_file(filepath: &str, content: &str) -> Result<()> {
    std::fs::write(filepath, content).context("Write file")
}

#[tool(
    struct_name = FileReader,
    name = "ead_file",
    description = "Read file and return its content",
    args = [
        "filepath" : {
            "type": "string",
            "description": "The file path to read",
        },
    ]
)]
pub fn read_file(filepath: &str) -> Result<String> {
    std::fs::read_to_string(filepath).context("Read file")
}